package org.example.backend.demos.web.NewsModule.controller;

import org.example.backend.demos.web.NewsModule.entity.News;
import org.example.backend.demos.web.NewsModule.service.NewsService;
import org.example.backend.demos.web.api.newsApi;
import org.example.backend.demos.web.result.Pages;
import org.example.backend.demos.web.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 新闻表(News)表控制层
 *
 * @author makejava
 * @since 2025-09-08 15:32:49
 */
@RestController
@RequestMapping("news")
public class NewsController implements newsApi {
    /**
     * 服务对象
     */
    @Autowired
    private NewsService newsService;

    /**
     * 分页查询
     *
     * @param news        筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     * 此部分用不了
     */
    @Override
    public ResponseEntity<Result> queryByPage_limit(News news, PageRequest pageRequest) {
        // PageRequest(int page, int size, Sort sort)传递三个参数
        return ResponseEntity.ok(null);
    }

    /**
     * 查询全部新闻信息
     * @return 信息
     */
    @Override
    public ResponseEntity<Result> Allnews() {
        List<News> news = this.newsService.queryAll();
        Result result = new Result<>(200, "查询成功(!null)", news);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * @param page 当前页面
     * @param size 页面大小
     * @return 信息
     */
    @Override
    public ResponseEntity<Result> queryByPage(int page, int size) {
        Page<News> news = newsService.GetProjectByPage(page, size);
        Result result = new Result<>(200, "查询成功(!null)", news);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 分页查询
     *
     * @param pages 对象
     * @return 信息
     */
    @Override
    public ResponseEntity<Result> queryByPage_Pages(Pages pages) {
        Pages queryByPage = this.newsService.queryByPage(pages);
        Result result = new Result<>(200, "查询成功(!null)", queryByPage);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    public ResponseEntity<Result> queryById(@PathVariable("id") Long id) {
        News news = this.newsService.queryById(id);
        Result result = new Result<>(200, "查询成功(!null)", news);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 新增数据
     *
     * @param news 实体
     * @return 新增结果
     */
    @Override
    public ResponseEntity<Result> add(News news) {
        System.currentTimeMillis();
        Date now = new Date();
        news.setPublishTime(now);
        news.setUpdateTime(now);
        News insert = this.newsService.insert(news);
        Result result = new Result<>(200, "新增成功(!null)", insert);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 更新数据
     *
     * @param news 实体
     * @return 更新结果
     */
    @Override
    public ResponseEntity<Result> edit(News news) {
        System.currentTimeMillis();
        Date now = new Date();
        news.setUpdateTime(now);
        News update = this.newsService.update(news);
        Result result = new Result<>(200, "更新成功(!null)", update);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Override
    public ResponseEntity<Result> deleteById(Long id) {
        boolean deleteById = this.newsService.deleteById(id);
        Result result = new Result<>(200, "成功/失败(true/false)", deleteById);
        return ResponseEntity.status(200).body(result);
    }

}

